<template>
	<view class="content">
		<view class="menu-list">
			<view class="menu-item">
				<view class="label">免打扰</view>
				<view class="value">
					<switch class="switch" type="switch" :checked="isLeaving" color="#FF76A8" @change="switchChange" />
				</view>
			</view>
			<view class="menu-item" @click="openWin('safe')">
				<view class="label">安全指南</view>
				<view class="value"></view>
				<view class="uni-icon uni-icon-arrowright"></view>
			</view>
			<view class="menu-item" @click="openWin('black_list')">
				<view class="label">黑名单</view>
				<view class="value"></view>
				<view class="uni-icon uni-icon-arrowright"></view>
			</view>
			<view class="menu-item" @click="openWin('about')">
				<view class="label">关于</view>
				<view class="value"></view>
				<view class="uni-icon uni-icon-arrowright"></view>
			</view>
			<view class="menu-item" @click="openProtocol">
				<view class="label">用户服务协议</view>
				<view class="value"></view>
				<view class="uni-icon uni-icon-arrowright"></view>
			</view>
			<view class="menu-item" @click="openPrivacy">
				<view class="label">用户隐私政策</view>
				<view class="value"></view>
				<view class="uni-icon uni-icon-arrowright"></view>
			</view>
		</view>
		<view class="menu-list">
			<view class="menu-item" @click="checkUpdate">
				<view class="label">当前版本号</view>
				<view class="value blue">{{version}}</view>
			</view>
		</view>
		<view class="btn-wrap">
			<button type="primary" @click="logout">退出登录</button>
			<button class="logoff" type="default" @click="logoff">注销用户</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLeaving: false,
				version: '',
			}
		},
		onLoad(options) {
			this.version = plus.runtime.version
			uni.getStorage({
				key: 'isLeaving',
				success: res => {
					this.isLeaving = res.data
				}
			})
		},
		onReady() {
			
		},
		methods: {
			switchChange: function (e) {
				this.isLeaving = e.target.value
				uni.setStorage({
					key: 'isLeaving',
					data: this.isLeaving,
					success: () => {
						console.log('免打扰状态已存储' + this.isLeaving);
					}
				});
			},
			
			openWin: function (url) {
				uni.navigateTo({
					url: url
				})
			},
			
			openProtocol: function() {
				uni.navigateTo({
					url: '../welcome/protocol'
				})
			},
			
			openPrivacy: function() {
				uni.navigateTo({
					url: '../welcome/privacy'
				})
			},
			
			logout: function () {
				this.$tim.logout().then(imResponse => {
					console.log('登出成功:', imResponse);
					try {
					    uni.removeStorageSync('isLeaving');
					    uni.removeStorageSync('myInfo');
						uni.removeStorageSync('kefuid');
						uni.removeStorageSync('sensitive');
						
						uni.reLaunch({
							url: '../welcome/welcome'
						});
					} catch (e) {
						console.warn(e)
					}
				}).catch(imError => {
					console.warn('登出失败:', imError);
				});
			},
			
			logoff: function () {
				uni.showModal({
					title: '提醒',
					content: '注销用户操作，将把您的用户信息从平台删除，注销之后无法找回，确定要注销用户吗？',
					success: res => {
						if (res.confirm) {
							this.$tim.logout().then(imResponse => {
								uni.showToast({
									title: '您的注销申请已提交审核，请耐心等待审核结果',
									position: 'bottom',
									icon: 'none'
								})
								console.log('登出成功:', imResponse);
								try {
								    uni.removeStorageSync('isLeaving');
								    uni.removeStorageSync('myInfo');
									uni.removeStorageSync('kefuid');
									uni.reLaunch({
										url: '../welcome/welcome'
									});
								} catch (e) {
									console.warn(e)
								}
							}).catch(imError => {
								console.warn('登出失败:', imError);
							});
						}
					}
				})
			},
			
			checkUpdate: function () {
				// #ifdef APP-PLUS
				let versionCode = parseInt(plus.runtime.versionCode);
				let versionString = plus.runtime.version;
				let versionArray = versionString.split('.');
				let versionBuild = 0;
				for (let i = 0; i < versionArray.length; i++) {
					let multiple = Math.pow(10, versionArray.length-1-i);
					versionBuild += versionArray[i]*multiple;
				}
				this.$api.api.ajaxData({
					method: 'V3.Update',
					versionid: plus.runtime.appid,
					version: versionCode,
					versionbuild: versionBuild,
				}, res => {
					if (res.status) {
						if (!res.data.update) {
							uni.showToast({
								title: '已经是最新版本',
								position: 'bottom',
								icon: 'none'
							});
							return false;
						}
						if (!!res.data.wgtUrl) {
							uni.downloadFile({
								url: res.data.wgtUrl,
								success: downloadResult => {  
									if (downloadResult.statusCode === 200) { 
										plus.runtime.install(downloadResult.tempFilePath, {
											force: false
										}, widgetInfo => {
											console.log(widgetInfo);
											plus.runtime.restart();
										}, err => {
											console.error(err);
										});
									}
								}
							});
						}
						if (!!res.data.pkgUrl) {
							uni.showModal({
								title: '更新提示',
								content: '有新版本更新~',
								success: res1 => {
									if (res1.confirm) {
										plus.runtime.openURL(res.data.pkgUrl);
									} else {
										plus.runtime.quit();
									}
								}
							})
						}
					} else {
						uni.showToast({
							title: res.msg,
							position: 'bottom',
							icon: 'none'
						});
					}
				}, err => {
					uni.showToast({
						title: '网络错误，请检查网络',
						position: 'bottom',
						icon: 'none'
					});
				})
				// #endif
			}
		}
	}
</script>

<style lang="scss">
	@import '../../common/common.scss';
	page {
		width: 100%;
		height: 100%;
	}
	.content {
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
		align-items: center;
		width: 100%;
		height: 100%;
	}
	.menu-list {
		width: 100%;
		background: #fff;
		margin-bottom: 20rpx;
		.menu-item {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			padding: 25rpx;
			& + .menu-item {
				border-top: 1rpx solid #f5f5f5;
			}
			.menu-icon {
				width: 40rpx;
				height: 40rpx;
			}
			.label {
				font-size: 30rpx;
				color: #333;
				margin-left: 10rpx;
			}
			.value {
				flex: 1;
				font-size: 28rpx;
				color: #999;
				margin: 0 10rpx;
				text-align: right;
				&.red {
					color: #FF71A5;
				}
				&.blue {
					color: #62cbfa;
				}
				switch {
					margin: 0;
				}
			}
			.switch {
				transform: scale(0.9);
				position: relative;
				right: -20rpx;
			}
			.uni-icon {
				font-size: 28rpx;
				color: #999;
			}
		}
	}
	
	.btn-wrap {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		align-items: center;
		flex: 1;
		width: 100%;
		margin-top: 50rpx;
		button {
			width: 70%;
			border-radius: 80rpx;
		}
		.logoff {
			width: 200rpx;
			font-size: 28rpx;
			color: #999;
			margin-bottom: 40rpx;
			background: none;
		}
	}
</style>
